//
//  JokeingViewController.m
//  播求
//
//  Created by MS on 15-9-17.
//  Copyright (c) 2015年 gengsensen. All rights reserved.
//

#import "JokeingViewController.h"
#import "JokeingTableViewCell.h"
#import "JokeingModel.h"
#import "CellDidSelectViewController.h"
@interface JokeingViewController ()

@end

@implementation JokeingViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    
    self.tableView=[[UITableView alloc]init];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    
    _tableView.autoresizesSubviews = NO;
    [self.view addSubview:_tableView];
    
    self.view.backgroundColor=[UIColor greenColor];
    
    self.tableView.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-49);
     //    去掉多余的小格子
    _tableView.tableFooterView = [[UIView alloc] init];
    _dataArr=[[NSMutableArray alloc]init];
    

    self.automaticallyAdjustsScrollViewInsets=NO;// 解决TableView错位的问题


    
}

-(void)viewWillAppear:(BOOL)animated{
    
    
    [DownLoadData getRequestType:Enum_JokePage withData:^(id obj, NSError *err) {
        
        if(obj)
        {
            
            self.dataArr=obj;
            
            [self.tableView reloadData];
            [self.tableView.footer endRefreshing];
        }
        else
        {
            UIAlertView * a = [[UIAlertView alloc] initWithTitle:@"提示" message:@"数据下载失败" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [a show];
            NSLog(@"段子---数据下载失败");
            a = nil;
        }

        
    } withPage:0];


}

#pragma mark---UITableViewDataSource,UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString * string = @"cellID";
    JokeingTableViewCell * cell =[self.tableView dequeueReusableCellWithIdentifier:string];
    if (cell == nil) {
        cell = [[JokeingTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    cell.delegate = self;
    
    cell.tag = indexPath.row+1;
    JokeingModel * model = self.dataArr[indexPath.row];
    [cell.personImageView setImageWithURL:[NSURL URLWithString:model.profile_image]];
    cell.nameLable.text = model.name;
    cell.nameLable.font = [UIFont systemFontOfSize:13];
    
    cell.dateLabel.text = model.create_time;
    cell.dateLabel.font = [UIFont systemFontOfSize:10];
    
    cell.loveLabel.text = model.love;
    cell.hateLabel.text = model.hate;
    
    cell.TextLabel.text = model.Mytext;
    
        return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JokeingModel * model = self.dataArr[indexPath.row];

    CGSize sise = [model.Mytext sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(320, 2000) lineBreakMode:NSLineBreakByCharWrapping];
    

    return sise.height+130;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
    JokeingModel * model = self.dataArr[indexPath.row];

    CellDidSelectViewController * cellDid = [[CellDidSelectViewController alloc]init];
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:cellDid];
    nav.navigationItem.title = @"百思不得";
    cellDid.httpString = model.weixin_url;
    
    [self.navigationController pushViewController:cellDid animated:YES];
    



}

-(void)DidSelectLoveButton:(UIButton *)sender{
    
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:@"谢谢您的赞👍" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
    [alert show];
    alert = nil;
   
    

}
-(void)DidSelectHateButton:(UIButton *)sender{

    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:@"您好狠心啊😒" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
    [alert show];
    alert = nil;


}
-(void)DidSelectShareButton:(UIButton *)sender{
    
    JokeingModel * model = self.dataArr[sender.tag-1];
    

    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"55ebb5e667e58e8639000bf7" shareText:model.Mytext shareImage:nil shareToSnsNames:@[UMShareToQQ,UMShareToSina,UMShareToSms,UMShareToRenren,UMShareToDouban,UMShareToEmail,UMShareToTencent,UMShareToWechatSession] delegate:nil];
    
    
}








@end
